#coding=utf-8
import Tkinter
import webbrowser
from base_client import base_client

def predicate_loginform( form ):
    if form.attrs.has_key("id") and form.attrs["id"]=="loginForm":
        return True
    return False

def predicate_editorform( form ):
    if form.attrs.has_key("id") and form.attrs["id"]=="editorForm":
        return True
    return False

def predicate_statusform( form ):
    if form.attrs.has_key("id") and form.attrs["id"]=="publisher_upload_form":
        return True
    return False

class client(base_client):
    def __init__(self, config=None ):
        if None==config:
            config = { 
                      "blog_user":"",
                      "blog_password":"",
                      "microblog_user":"",
                      "microblog_password":"",
                      "blogControl":"所有人可见"
             }
        base_client.__init__( self, config )

    def __blog_login(self):
        self.browser.open( 'http://renren.com/' )
        self.browser.select_form( predicate=predicate_loginform )
        self.browser["email"] = self.var_blog_user.get()
        self.browser["password"] = self.var_blog_password.get()
        self.browser.submit()

    def __microblog_login(self):
        self.browser.open( 'http://renren.com/' )
        self.browser.select_form( predicate=predicate_loginform )
        self.browser["email"] = self.var_microblog_user.get()
        self.browser["password"] = self.var_microblog_password.get()
        self.browser.submit()

    def post_blog(self, title, content ):
        self.btn_blog_state.config( bg="yellow" )
        try:
            self.__blog_login()
            self.browser.open( 'http://blog.renren.com/NewEntry.do' )
            self.browser.select_form( predicate=predicate_editorform )
            self.browser["title"] = title
            self.browser["body"] = content
            
            if "所有人可见"==self.var_blogcontrol.get():
                self.browser["blogControl"] = ["99"]
            elif "好友可见"==self.var_blogcontrol.get():
                self.browser["blogControl"] = ["1"]
            else:
                self.browser["blogControl"] = ["-1"]

            self.browser.submit()
        except:
            self.btn_blog_state.config( bg="red" )        
        else:
            self.btn_blog_state.config( bg="blue" )

    # not ready
    def post_microblog(self, content):
        self.btn_microblog_state.config( bg="yellow" )
        try:
            self.__microblog_login()
            self.browser.open( 'http://www.renren.com/home#//status/status?from=homeleft' )
            self.browser.select_form( predicate=predicate_statusform )
            self.browser["status"] = content
            self.browser.submit()
        except:
            self.btn_microblog_state.config( bg="red" )        
        else:
            self.btn_microblog_state.config( bg="blue" )
    
    def display_blog_option(self, father):
        base_client.display_blog_option( self, father, "人人", self.call_browser )
        self.var_blogcontrol = Tkinter.StringVar()
        self.var_blogcontrol.set( self.config["blogControl"] )
        Tkinter.OptionMenu( father, self.var_blogcontrol, "所有人可见","好友可见","仅自己可见" ).grid( row=0, column=6, sticky=Tkinter.W )

    def display_microblog_option(self, father):
        base_client.display_microblog_option( self, father, "人人", self.call_browser )

    def call_browser(self):
        webbrowser.open_new_tab('http://www.renren.com/home')
    
    def update_config(self):
        if "var_blog_user" in dir(self):
            self.config["blog_user"] = self.var_blog_user.get()
            self.config["blog_password"] = self.var_blog_password.get()
            self.config["blogControl"]=self.var_blogcontrol.get()
        if "var_microblog_user" in dir(self):
            self.config["microblog_user"] = self.var_microblog_user.get()
            self.config["microblog_password"] = self.var_microblog_password.get()
        